import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

// export default new Router({
//   mode: "history",
const routes = [
  {
    path: '/',
    name: 'index',
    component: () => import('./components/index'),
    meta: {
      title: "index"
    }
  },
  {
    path: '/verify',
    name: 'verify',
    component: () => import('./components/verify'),
    meta: {
      title: "verify"
    }
  },
  {
    path: '/register',
    name: 'register',
    component: () => import('./components/register'),
    meta: {
      title: "register"
    }
  },
  {
    path: '/feedback',
    name: 'feedback',
    component: () => import('./components/feedback'),
    meta: {
      title: "feedback"
    }
  },
  {
    path: '/GoodsCar',
    name: 'GoodsCar',
    component: () => import('./components/GoodsCar'),
    meta: {
      title: "GoodsCar"
    }
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('./components/login'),
    meta: {
      title: "login"
    }
  },
  {
    path: '/Recharge',
    name: 'Recharge',
    component: () => import('./components/Recharge'),
    meta: {
      title: "Recharge"
    }
  },
  {
    path: '/findUser',
    name: 'findUser',
    component: () => import('./components/FindUser'),
    meta: {
      title: "findUser"
    }
  },
  {
    path: '/orderList',
    name: 'OrderList',
    component: () => import('./components/Order'),
    meta: {
      title: "OrderList"
    }
  },
  {
    path: '/GoodsDetail/:id',
    name: 'GoodsDetail',
    component: () => import('./components/GoodsDetail'),
    meta: {
      title: "GoodsDetail"
    }
  },
  {
    path: '/admin',
    name: 'admin',
    component: () => import('./components/admin/admin'),
    meta: {
      title: "admin"
    }
  },
  {
    path: '/admin/home',
    name: 'home',
    component: () => import('./components/admin/homePage'),
    meta: {
      title: "home"
    }
  }
]

const router = new Router({
  routes,
  mode: 'history',
  linkActiveClass: 'active'
})
router.beforeEach((to, from, next) => {
  /* 路由发生变化修改页面title */
  if (to.meta.title) {
    document.title = to.meta.title
  }
  next()
})

router.beforeEach((to, form, next) => {
  //访问下面路由直接放行
  if (to.path == "/" || to.path == "/login" || to.path == "/admin" || to.path == "/register" || to.path == "/findUser" || to.path == "/feedback") {
    next()
  } else {
    const reg = /\/admin\/[a-zA-z]+[_-]?/ //对请求路径进行判断
    if (reg.test(to.path)) {
      const admin_token = localStorage.getItem("admin_token")
      if (admin_token == "" || admin_token == null || admin_token == '') { //如果admin为空 就是用户没有登陆
        next("/admin") //跳转到 admin login 页面
      } else {
        next(); //登陆后就放行
      }
    } else {
      // 校验用户的，不受管理员影响
      const front_token = localStorage.getItem("front_token")
      // front的需要权限的url
      if (front_token == "" || front_token == null || front_token == '') {
        next("/login")
      } else {
        next()
      }
    }
  }
})

export default router;
